package com.ychug.framework.plugin.file;

import com.ychug.common.entity.VirtualFile;
import com.ychug.common.exception.GlobalFileException;
import com.ychug.file.ApiClient;
import com.ychug.file.LocalApiClient;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2019/2/11 14:26
 * @since 1.8
 */
public class BaseFileUploader {

    ApiClient getApiClient(String uploadType) {
//        SysConfigService configService = SpringContextHolder.getBean(SysConfigService.class);
//        Map<String, Object> config = configService.getConfigs();
        String storageType = uploadType;
//        if (null == config || StringUtils.isEmpty((storageType = (String) config.get(ConfigKeyEnum.STORAGE_TYPE.getKey())))) {
//            throw new ZhydException("[文件服务]当前系统暂未配置文件服务相关的内容！");
//        }
        Map<String, Object> config = new HashMap<>();
        config.put("localFileUrl","http://localhost:8081/upload");
        config.put("localFilePath","/news");

        ApiClient res = null;
        switch (storageType) {
            case "local":
                String localFileUrl = "http://localhost:8081/upload"; //(String) config.get(ConfigKeyEnum.LOCAL_FILE_URL.getKey()),
                 String localFilePath = "E:\\wwwroot\\mcpcms\\upload\\news\\";//(String) config.get(ConfigKeyEnum.LOCAL_FILE_PATH.getKey());
                res = new LocalApiClient().init(localFileUrl, localFilePath, uploadType);
                break;
            case "qiniu":
                break;
            case "aliyun":
                break;
            case "youpaiyun":
                break;
            default:
                break;
        }
        if (null == res) {
            throw new GlobalFileException("[文件服务]当前系统暂未配置文件服务相关的内容！");
        }
        return res;
    }

    VirtualFile saveFile(VirtualFile virtualFile, boolean save, String uploadType) {
//        if (save) {
//            BizFileService fileService = SpringContextHolder.getBean(BizFileService.class);
//            try {
//                SysConfigService configService = SpringContextHolder.getBean(SysConfigService.class);
//                Map<String, Object> config = configService.getConfigs();
//                String storageType = (String) config.get(ConfigKeyEnum.STORAGE_TYPE.getKey());
//
//                BizFile fileInfo = BeanConvertUtil.doConvert(virtualFile, BizFile.class);
//                User sessionUser = SessionUtil.getUser();
//                fileInfo.setUserId(null == sessionUser ? null : sessionUser.getId());
//                fileInfo.setUploadType(uploadType);
//                fileInfo.setStorageType(storageType);
//                fileService.insert(new File(fileInfo));
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        return virtualFile;
    }

    boolean removeFile(ApiClient apiClient, String filePath, String uploadType) {
//        BizFileService fileService = SpringContextHolder.getBean(BizFileService.class);
//        File file = fileService.selectFileByPathAndUploadType(filePath, uploadType);
//        String fileKey = filePath;
//        if (null != file) {
//            fileKey = file.getFilePath();
//        }
        return apiClient.removeFile(null);
    }

}
